package com.happly.happlycloud.controller.portal;

import com.github.pagehelper.PageInfo;
import com.happly.happlycloud.common.ServerResponse;
import com.happly.happlycloud.service.IProductService;
import com.happly.happlycloud.vo.ProductDetailVo;
import com.happly.happlycloud.vo.UserDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product/")
@Api("用户商品")
public class ProductController {

    @Autowired
    private IProductService iProductService;



    @GetMapping("detail.do")
    @ApiOperation("添加")
    public ServerResponse<ProductDetailVo> detail(Integer productId){
        return iProductService.getProductDetail(productId);
    }

    @GetMapping("/{productId}")
    @ApiOperation("添加")
    public ServerResponse<ProductDetailVo> detailRESTful(@PathVariable("productId") Integer productId){
        return iProductService.getProductDetail(productId);
    }

    @GetMapping("userdetail.do")
    @ApiOperation("添加")
    public ServerResponse<UserDetailVo> userDetail(Integer productId){
        return iProductService.getUserDetail(productId);
    }

    @GetMapping("/userdetail/{productId}")
    @ApiOperation("添加")
    public ServerResponse<UserDetailVo> userDetailRESTful(@PathVariable Integer productId){
        return iProductService.getUserDetail(productId);
    }

    @GetMapping("list.do")
    @ApiOperation("添加")
    public ServerResponse<PageInfo> list(@RequestParam(value = "keyword",required = false)String keyword,
                                         @RequestParam(value = "categoryId",required = false)Integer categoryId,
                                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                         @RequestParam(value = "orderBy",defaultValue = "") String orderBy){
        return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
    }

    @GetMapping("/{keyword}/{categoryId}/{pageNum}/{pageSize}/{orderBy}")
    @ApiOperation("添加")
    public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "keyword")String keyword,
                                         @PathVariable(value = "categoryId")Integer categoryId,
                                         @PathVariable(value = "pageNum") Integer pageNum,
                                         @PathVariable(value = "pageSize") Integer pageSize,
                                         @PathVariable(value = "orderBy") String orderBy){
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageNum = 10;
        }
        if (StringUtils.isBlank(orderBy)) {
            orderBy = "price_asc";
        }
        return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
    }

    @GetMapping("/keyword/{keyword}/{pageNum}/{pageSize}/{orderBy}")
    @ApiOperation("添加")
    public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "keyword")String keyword,
                                                @PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize,
                                                @PathVariable(value = "orderBy") String orderBy){
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if(StringUtils.isBlank(orderBy)){
            orderBy = "price_asc";
        }

        return iProductService.getProductByKeywordCategory(keyword,null,pageNum,pageSize,orderBy);
    }


    @GetMapping("/category/{categoryId}/{pageNum}/{pageSize}/{orderBy}")
    @ApiOperation("添加")
    public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "categoryId")Integer categoryId,
                                                @PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize,
                                                @PathVariable(value = "orderBy") String orderBy){
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if(StringUtils.isBlank(orderBy)){
            orderBy = "price_asc";
        }

        return iProductService.getProductByKeywordCategory("",categoryId,pageNum,pageSize,orderBy);
    }
}
